package fr.nts.framework.core.commun.tool;

import org.apache.commons.lang.StringUtils;

/**
 * Méthode utilitaire pour le calcul de temps/date
 * 
 * @author sco
 * @version 1.0.0
 */
public class TimeUtils{

	private final static String ZERO = "0";
	private final static String DOUBLE_ZERO = "00";
	private final static String H = "H";
	private final static String MN = "mn";
	private final static String S = "s";
	private final static String MS = "ms";
	
	/**
	 * Renvoie une durée au format: mm:HH:ss
	 * 
	 * @param startMs La date de début en ms
	 * @param endMs La date de fin en ms
	 * @return Le temps écoulé
	 */
	public static String getTime(long startMs,
			                     long endMs){
		long lastMs = endMs - startMs;
		
		long timeInS = lastMs / 1000;
		long timeInMs = lastMs % 1000;
		long timeInMin = timeInS / 60;
		timeInS = timeInS % 60;
		long timeInH = timeInMin / 60;
		timeInMin = timeInMin % 60;
		
		return new StringBuilder().append(timeInH < 10? ZERO: StringUtils.EMPTY)
		                          .append(timeInH)
		                          .append(H)
		                          .append(timeInMin < 10? ZERO: StringUtils.EMPTY)
		                          .append(timeInMin)
		                          .append(MN)
		                          .append(timeInS < 10? ZERO: StringUtils.EMPTY)
		                          .append(timeInS)
		                          .append(S)
		                          .append(timeInMs < 10? DOUBLE_ZERO: timeInMs < 100? ZERO: StringUtils.EMPTY)
		                          .append(timeInMs)
		                          .append(MS)
		                          .toString();
	}
}